# -*- coding:utf-8 -*-
"""
@Author : 审心
@Time : 2023/5/27 15:55
"""
from typing import List

from xlwings import Sheet

from common.utils import ExcelUtil
from common.utils.ExcelUtil import XwRowMap, xw_get_has_content_row, xw_get_last_used_column


def del_row(sht: Sheet, row_num_list: List[int]):
    """
    :param sht: sheet 页对象
    :param row_num_list:
    :return:
    """
    for row in sorted(row_num_list, reverse=True):
        ExcelUtil.xw_delete_row_by_row_num(sht, row)
        ExcelUtil.xw_del_sht_img_by_name(sht, f"Picture {row - 1}")


def get_ignore_del_row_num(row_map_list: List[XwRowMap], amount_top_r: int, search_people_top_r: int):
    """
    获取忽略要删除的行号
    :param row_map_list:
    :param amount_top_r: 交易金额排行百分比
    :param search_people_top_r: 搜索人数排行百分比
    :return:
    """
    result = set()
    if len(row_map_list) > 0:
        range_ = row_map_list[0].row.sheet.range("1:1").value
        index = range_.index("交易金额")
        row_map_list.sort(key=lambda obj: obj.row.value[index], reverse=True)
        for row in row_map_list[:int(len(row_map_list) * (amount_top_r / 100))]:
            result.add(row.num)
        index = range_.index("客单价")
        row_map_list.sort(key=lambda obj: obj.row.value[index], reverse=True)
        for row in row_map_list[:int(len(row_map_list) * (search_people_top_r / 100))]:
            result.add(row.num)
    return list(result)


def get_sheet_to_row_map_list(sht: Sheet, calc_row: str, calc_col: int) -> List[XwRowMap]:
    """
    获取表格正文范围对象
    :param sht: sheet 对象
    :param calc_row: 计算最后一行使用的行号
    :param calc_col: 计算最后一列使用的列索引
    :return:
    """
    last_row_num = xw_get_has_content_row(sht, calc_row)
    last_col_num = xw_get_last_used_column(sht, calc_col)
    return XwRowMap.builder_by_rows(sht.range(f"A2:{last_col_num + str(last_row_num)}"))
